package com.yicong.ants.ui.me.account;

import android.view.View;

import com.cchao.simplelib.core.JsonHelper;
import com.cchao.simplelib.core.RxBus;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.me.UserIncome;
import com.yicong.ants.databinding.IncomeEditActvityBinding;
import com.yicong.ants.manager.UiAssistant;
import com.yicong.ants.manager.http.RetrofitHelper;

/**
 * 收款账号编辑
 *
 * @author cchao
 * @since 2019-12-26
 */
public class IncomeEditActivity extends BaseTitleBarActivity<IncomeEditActvityBinding> {
    UserIncome mUserIncome;

    @Override
    protected int getLayout() {
        return R.layout.income_edit_actvity;
    }

    @Override
    protected void initEventAndData() {
        setTitleText("收款信息");
        addTitleMenuItem(UiAssistant.getAtyMenuText(mContext, "保存"), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onUpdateInfo();
            }
        });
        onLoadData();
    }

    private void onUpdateInfo() {

        View checkedView=findViewById(mDataBind.channelGroup.getCheckedRadioButtonId());
        int index=mDataBind.channelGroup.indexOfChild(checkedView);

        mUserIncome.setChannel(String.valueOf(index + 1));
        mUserIncome.setAccount(mDataBind.inputAccount.getText().toString());
        mUserIncome.setName(mDataBind.inputName.getText().toString());
        showProgress();
        addSubscribe(RetrofitHelper.getApis().updateIncome(JsonHelper.toMap(JsonHelper.toJson(mUserIncome), String.class))
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                hideProgress();
                showToast(respBean.getMsg());
                if (respBean.isCodeFail()) {
                    return;
                }
                RxBus.get().postEvent(Constants.Event.Update_Income_Info);
                finish();
            }, RxHelper.getHideProgressConsumer(this)));
    }

    @Override
    protected void onLoadData() {
        switchView(LOADING);
        addSubscribe(RetrofitHelper.getApis().getIncomeInfo()
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                // 为空
                if (respBean.code.equals("20")) {
                    mUserIncome = new UserIncome();
                    mDataBind.channelGroup.check(mDataBind.channelGroup.getChildAt(0).getId());
                    switchView(CONTENT);
                    return;
                }
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    switchView(NET_ERROR);
                    return;
                }
                switchView(CONTENT);
                mUserIncome = respBean.getData();
                mDataBind.setBean(mUserIncome);
                mDataBind.channelGroup.check(mDataBind.channelGroup.getChildAt(Integer.parseInt(mUserIncome.getChannel())-1).getId());

            }, RxHelper.getSwitchErrorConsumer(this)));
    }
}
